<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>检查对象是否继承或者实现（implement）此类</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.is-a.html">« is_a</a></li>
      <li style="float: right;"><a href="function.method-exists.html">method_exists »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.classobj.html">类/对象 函数</a></li>
    <li>检查对象是否继承或者实现（implement）此类</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.is-subclass-of" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">is_subclass_of</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">is_subclass_of</span> &mdash; <span class="dc-title">检查对象是否继承或者实现（implement）此类</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.is-subclass-of-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>is_subclass_of</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$object_or_class</code></span>, <span class="methodparam"><span class="type">string</span> <code class="parameter">$class</code></span>, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$allow_string</code><span class="initializer"> = <strong><code>true</code></strong></span></span>): <span class="type">bool</span></div>

  <p class="para rdfs-comment">
   检查指定的 <code class="parameter">object_or_class</code> 是否继承或者实现（implement） <code class="parameter">class</code>。
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.is-subclass-of-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">object_or_class</code></dt>

     <dd>

      <p class="para">
       类名或者对象实例。如果类不存在，也不会报错。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">class</code></dt>

     <dd>

      <p class="para">
       类名
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">allow_string</code></dt>

     <dd>

      <p class="para">
       如果此参数设置为 false，将不允许将字符串类名传递给 
       <code class="parameter">object_or_class</code>。如果类不存在，这也可以防止调用自动加载器。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.is-subclass-of-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   如果对象 <code class="parameter">object_or_class</code> 属于 <code class="parameter">class</code> 的子类，则返回 
   <strong><code>true</code></strong>，否则返回 <strong><code>false</code></strong>。
  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.is-subclass-of-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-5040">
    <p><strong>示例 #1 <span class="function"><strong>is_subclass_of()</strong></span> 示例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;定义类<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">WidgetFactory<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;var&nbsp;</span><span style="color: #0000BB">$oink&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'moo'</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;定义子类<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">WidgetFactory_Child&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">WidgetFactory<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;var&nbsp;</span><span style="color: #0000BB">$oink&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'oink'</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;创建新对象<br /></span><span style="color: #0000BB">$WF&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">WidgetFactory</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$WFC&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">WidgetFactory_Child</span><span style="color: #007700">();<br /><br />if&nbsp;(</span><span style="color: #0000BB">is_subclass_of</span><span style="color: #007700">(</span><span style="color: #0000BB">$WFC</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'WidgetFactory'</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"yes,&nbsp;\$WFC&nbsp;is&nbsp;a&nbsp;subclass&nbsp;of&nbsp;WidgetFactory\n"</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"no,&nbsp;\$WFC&nbsp;is&nbsp;not&nbsp;a&nbsp;subclass&nbsp;of&nbsp;WidgetFactory\n"</span><span style="color: #007700">;<br />}<br /><br /><br />if&nbsp;(</span><span style="color: #0000BB">is_subclass_of</span><span style="color: #007700">(</span><span style="color: #0000BB">$WF</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'WidgetFactory'</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"yes,&nbsp;\$WF&nbsp;is&nbsp;a&nbsp;subclass&nbsp;of&nbsp;WidgetFactory\n"</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"no,&nbsp;\$WF&nbsp;is&nbsp;not&nbsp;a&nbsp;subclass&nbsp;of&nbsp;WidgetFactory\n"</span><span style="color: #007700">;<br />}<br /><br /><br />if&nbsp;(</span><span style="color: #0000BB">is_subclass_of</span><span style="color: #007700">(</span><span style="color: #DD0000">'WidgetFactory_Child'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'WidgetFactory'</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"yes,&nbsp;WidgetFactory_Child&nbsp;is&nbsp;a&nbsp;subclass&nbsp;of&nbsp;WidgetFactory\n"</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"no,&nbsp;WidgetFactory_Child&nbsp;is&nbsp;not&nbsp;a&nbsp;subclass&nbsp;of&nbsp;WidgetFactory\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
yes, $WFC is a subclass of WidgetFactory
no, $WF is not a subclass of WidgetFactory
yes, WidgetFactory_Child is a subclass of WidgetFactory
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-5041">
    <p><strong>示例 #2 <span class="function"><strong>is_subclass_of()</strong></span> 使用接口示例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;定义接口<br /></span><span style="color: #007700">interface&nbsp;</span><span style="color: #0000BB">MyInterface<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">MyFunction</span><span style="color: #007700">();<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;定义实现了接口的类<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">MyClass&nbsp;</span><span style="color: #007700">implements&nbsp;</span><span style="color: #0000BB">MyInterface<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">MyFunction</span><span style="color: #007700">()<br />&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #DD0000">"MyClass&nbsp;Implements&nbsp;MyInterface!"</span><span style="color: #007700">;<br />&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;实例化对象<br /></span><span style="color: #0000BB">$my_object&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MyClass</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;自&nbsp;5.3.7&nbsp;起可用<br /><br />//&nbsp;使用类的对象实例进行检查<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">is_subclass_of</span><span style="color: #007700">(</span><span style="color: #0000BB">$my_object</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'MyInterface'</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Yes,&nbsp;\$my_object&nbsp;is&nbsp;a&nbsp;subclass&nbsp;of&nbsp;MyInterface\n"</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"No,&nbsp;\$my_object&nbsp;is&nbsp;not&nbsp;a&nbsp;subclass&nbsp;of&nbsp;MyInterface\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;使用字符串类名进行检查<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">is_subclass_of</span><span style="color: #007700">(</span><span style="color: #DD0000">'MyClass'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'MyInterface'</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Yes,&nbsp;MyClass&nbsp;is&nbsp;a&nbsp;subclass&nbsp;of&nbsp;MyInterface\n"</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"No,&nbsp;MyClass&nbsp;is&nbsp;not&nbsp;a&nbsp;subclass&nbsp;of&nbsp;MyInterface\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Yes, $my_object is a subclass of MyInterface
Yes, MyClass is a subclass of MyInterface
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.is-subclass-of-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: <p class="para">如果此类不是已知类，使用此函数会使用任何已注册的
<a href="language.oop5.autoload.html" class="link">autoloader</a>。</p></p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.is-subclass-of-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.get-class.html" class="function" rel="rdfs-seeAlso">get_class()</a> - 返回对象的类名</span></li>
    <li class="member"><span class="function"><a href="function.get-parent-class.html" class="function" rel="rdfs-seeAlso">get_parent_class()</a> - 检索对象或者类的父级类名</span></li>
    <li class="member"><span class="function"><a href="function.is-a.html" class="function" rel="rdfs-seeAlso">is_a()</a> - 如果对象属于该类或该类是此对象的父类则返回 true</span></li>
    <li class="member"><span class="function"><a href="function.class-parents.html" class="function" rel="rdfs-seeAlso">class_parents()</a> - 返回指定类的父类</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>